using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public class CreateRoleCheck : MonoBehaviour
{
	
	public UIInput userNameInput;
	public GameObject popupWindow;
	UILabel userNameText;
	int maxChars = 8;
	PopupWindow popup;
	
	void Awake ()
	{
		MySocket.getInstance ().addEventListener ((int)SystemMsgFlag.NetMessageSC.NM_SC_ROLE_CREATE_ACK, ListenToServer);
	}

	// Use this for initialization
	void Start ()
	{
		if (userNameInput == null) {
			Debug.Log ("Input of userName not reference");
		}
		
		userNameInput.label.text = "";
		userNameInput.maxChars = maxChars;
		userNameText = userNameInput.label;
		popup = popupWindow.GetComponent<PopupWindow> ();
	}
	
	void OnClick ()
	{
		string userName = userNameText.text;
		if (!SensitiveWordsCheck (userName)) {
			Debug.Log ("Your name has SensitiveWords");
			//TODO
			return;
		}
		
		if (userName == "") {
			Debug.Log ("Please enter your name");
			popupWindow.SetActive (true);
			popup.ShowAlertMessage (PopupWindow.AlertMessageType.PlayerNameIsEmpty);
			return;
		}
		
		if (!ValidationCheck (userName)) {
			Debug.Log ("You can only Enter Numbers,English words or Chinese words");
			popupWindow.SetActive (true);
			popup.ShowAlertMessage (PopupWindow.AlertMessageType.PlayerNameIsIllegal);
			return;
		}
		
		if (!UserExitsCheck (userName)) {
			Debug.Log ("This name has been used.");
			//TODO
			return;
		}
		CreateRoleInfo.GetInstance ().PlayerName = userNameInput.text;
		SendInfoToServer ();
		//SceneManagement.GetInstance().SwitchScene("main");
	}
	
	
	
	//The check of sensitive words
	bool SensitiveWordsCheck (string userName)
	{
		//TODO
		return true;
	}
	
	//Check of words which has entered;
	bool ValidationCheck (string userName)
	{ 
		//string RegexTest = "^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$";
		string RegexTest = "^[\u4E00-\u9FA5A-Za-z0-9_]+$";
		return Regex.IsMatch (userName, RegexTest);
	}
	
	//check whether the name exits or not
	bool UserExitsCheck (string userName)
	{
		//TODO
		return true;
	}
	
	void ListenToServer (System.Object getter, DispatchEventArgs e)
	{
		ByteArrayEx byteArray = new ByteArrayEx (e.data [0] as byte[]);
		
		byte messageId = byteArray.readByte ();
		if (messageId == (byte)CustomToClientFlag.RequestResult.success) {
			//ModelLocator.getInstance ().dispacthEvent (CustomToClientFlag.SERVER_CUSTOMMSG_ADD_MAINROLE, new object[]{byteArray});
			PlayerData.Instance ().initDataArray = new ByteArrayEx (byteArray.byteArray);
			SceneManagement.GetInstance ().SwitchScene ("main",true);
		} else if (messageId == (byte)CustomToClientFlag.RequestResult.failed) {
			Debug.Log ("创建角色失败");
			if (byteArray.readInt () == (int)CustomToClientFlag.CreateRoleFailedMessage.roleHasExit) {
				popup.ShowAlertMessage (" Name Has Exited ");
			}
		}
	}
	
	/// <summary>
	/// 向服务器发送请求.
	/// </summary>
	void SendInfoToServer ()
	{
		ByteArrayEx byteArray = new ByteArrayEx ();
		
		byteArray.writeInt ((int)SystemMsgFlag.NetMessageCS.NM_CS_ROLE_CREATE_SYN);
		byteArray.writeMultiByteEx (CreateRoleInfo.GetInstance ().PlayerName);							//s玩家姓名s
		byteArray.writeByte ((byte)CreateRoleInfo.GetInstance ().Profession);							//s职业s
		byteArray.writeByte ((byte)CreateRoleInfo.GetInstance ().Sex);									//s性别s
		byteArray.writeMultiByteEx (CreateRoleInfo.GetInstance ().HeadPicName);							//s头像名s
		
		MySocket.getInstance ().Async_Send (byteArray.byteArray);
	}
	
	/// <summary>
	/// 移除掉这个事件.
	/// </summary>
	void OnDestroy ()
	{
		MySocket.getInstance ().removeEventListener ((int)SystemMsgFlag.NetMessageSC.NM_SC_ROLE_CREATE_ACK, ListenToServer);
	}
}
